package com.sansan.qiangji.realize.report;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.warehouse.pullOrder.PullOrderBoxDetailListVO;
import com.sansan.qiangji.VO.warehouse.pullOrder.PullOrderBoxDetailVO;
import com.sansan.qiangji.VO.warehouse.pushOrder.PushOrderBoxDetailListVO;
import com.sansan.qiangji.VO.warehouse.pushOrder.PushOrderBoxDetailVO;
import com.sansan.qiangji.entity.warehouse.PullOrder;
import com.sansan.qiangji.entity.warehouse.PullOrderBoxDetail;
import com.sansan.qiangji.entity.warehouse.PushOrderBoxDetail;
import com.sansan.qiangji.entity.warehouse.PushOrderDetail;
import com.sansan.qiangji.entity.warehouse.example.PullOrderBoxDetailExample;
import com.sansan.qiangji.entity.warehouse.example.PushOrderBoxDetailExample;
import com.sansan.qiangji.form.warehouse.PullOrderBoxDetailListForm;
import com.sansan.qiangji.form.warehouse.PushOrderBoxDetailListForm;
import com.sansan.qiangji.service.inter.warehouse.PullOrderBoxDetailService;
import com.sansan.qiangji.service.inter.warehouse.PushOrderBoxDetailService;
import com.sansan.qiangji.utils.DateUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WarehouseRreportReal {
    @Autowired
    private PushOrderBoxDetailService pushOrderBoxDetailService;

    @Autowired
    private PullOrderBoxDetailService pullOrderBoxDetailService;


    //入库详情明细报表
    public PushOrderBoxDetailListVO pushBoxDetailReport(PushOrderBoxDetailListForm form){
        PushOrderBoxDetailExample pushOrderBoxDetailExample = new PushOrderBoxDetailExample();
        PushOrderBoxDetailExample.Criteria criteria = pushOrderBoxDetailExample.createCriteria();
        if(form.getPushTime()!=null&&form.getPushTime().size()!=0){
            String startTime = form.getPushTime().get(0)+" 00:00:00";
            String endTime = form.getPushTime().get(1)+" 23:59:59";
            Date start = DateUtil.string2Date(startTime,"yyyy-MM-dd HH:mm:ss");
            Date end = DateUtil.string2Date(endTime,"yyyy-MM-dd HH:mm:ss");
            criteria.andPushTimeBetween(start,end);
        }
        if(form.getProductCode()!=null&&!"".equals(form.getProductCode())){
            criteria.andProductCodeLike("%"+form.getProductCode()+"%");
        }
        if(form.getProductName()!=null&&!"".equals(form.getProductName())){
            criteria.andProductNameLike("%"+form.getProductName()+"%");
        }
        if(form.getBoxDetailStatus()!=null&&!"".equals(form.getBoxDetailStatus())){
            criteria.andBoxDetailStatusEqualTo(form.getBoxDetailStatus());
        }
        if(form.getPushOrderDetailCode()!=null&&!"".equals(form.getPushOrderDetailCode())){
            criteria.andPushOrderDetailCodeLike("%"+form.getPushOrderDetailCode()+"%");
        }
        int pageNum = form.getPageNum();
        int pageSize = form.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<PushOrderBoxDetail> boxDetails = pushOrderBoxDetailService.selectByExample(pushOrderBoxDetailExample);
        PageResult<PushOrderBoxDetail> pushOrderBoxDetailPageResult = PageUtil.getPageResult(new PageInfo<>(boxDetails));
        PushOrderBoxDetailListVO pushOrderBoxDetailListVO = new PushOrderBoxDetailListVO();
        BeanUtils.copyProperties(pushOrderBoxDetailPageResult,pushOrderBoxDetailListVO);
        List<PushOrderBoxDetailVO> pushOrderBoxDetailVOS = new ArrayList<>();
        for(PushOrderBoxDetail detail : pushOrderBoxDetailPageResult.getContent()){
            PushOrderBoxDetailVO pushOrderBoxDetailVO = new PushOrderBoxDetailVO();
            BeanUtils.copyProperties(detail,pushOrderBoxDetailVO);
            pushOrderBoxDetailVOS.add(pushOrderBoxDetailVO);
        }
        pushOrderBoxDetailListVO.setPushOrderBoxDetailVOS(pushOrderBoxDetailVOS);
        return pushOrderBoxDetailListVO;

    }

    //出库详情明细报表
    public PullOrderBoxDetailListVO pullBoxDetailReport(PullOrderBoxDetailListForm form){
        PullOrderBoxDetailExample pullOrderBoxDetailExample = new PullOrderBoxDetailExample();
        PullOrderBoxDetailExample.Criteria criteria = pullOrderBoxDetailExample.createCriteria();
        if(form.getPullTime()!=null&&form.getPullTime().size()!=0){
            String startTime = form.getPullTime().get(0)+" 00:00:00";
            String endTime = form.getPullTime().get(1)+" 23:59:59";
            Date start = DateUtil.string2Date(startTime,"yyyy-MM-dd HH:mm:ss");
            Date end = DateUtil.string2Date(endTime,"yyyy-MM-dd HH:mm:ss");
            criteria.andPullTimeBetween(start,end);
        }
        if(form.getProductCode()!=null&&!"".equals(form.getProductCode())){
            criteria.andProductCodeLike("%"+form.getProductCode()+"%");
        }
        if(form.getProductName()!=null&&!"".equals(form.getProductName())){
            criteria.andProductNameLike("%"+form.getProductName()+"%");
        }
        if(form.getBoxDetailStatus()!=null&&!"".equals(form.getBoxDetailStatus())){
            criteria.andBoxDetailStatusEqualTo(form.getBoxDetailStatus());
        }
        if(form.getPullOrderDetailCode()!=null&&!"".equals(form.getPullOrderDetailCode())){
            criteria.andPullOrderDetailCodeLike("%"+form.getPullOrderDetailCode()+"%");
        }
        int pageNum = form.getPageNum();
        int pageSize = form.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<PullOrderBoxDetail> boxDetails = pullOrderBoxDetailService.selectByExample(pullOrderBoxDetailExample);
        PageResult<PullOrderBoxDetail> pullOrderBoxDetailPageResult = PageUtil.getPageResult(new PageInfo<>(boxDetails));
        PullOrderBoxDetailListVO pullOrderBoxDetailListVO = new PullOrderBoxDetailListVO();
        BeanUtils.copyProperties(pullOrderBoxDetailPageResult,pullOrderBoxDetailListVO);
        List<PullOrderBoxDetailVO> pullOrderBoxDetailVOS = new ArrayList<>();
        for(PullOrderBoxDetail detail : pullOrderBoxDetailPageResult.getContent()){
            PullOrderBoxDetailVO pullOrderBoxDetailVO = new PullOrderBoxDetailVO();
            BeanUtils.copyProperties(detail,pullOrderBoxDetailVO);
            pullOrderBoxDetailVOS.add(pullOrderBoxDetailVO);
        }
        pullOrderBoxDetailListVO.setPullOrderBoxDetailVOS(pullOrderBoxDetailVOS);
        return pullOrderBoxDetailListVO;

    }

    //工资报表
    public void wageAccountReport(){

    }



}
